<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>class IPSocket - socket: Ruby Standard Library Documentation</title>


<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>

<script src="./js/jquery-3.2.0.min.js"></script>

<script src="./js/vue.min.js"></script>
<script src="./js/js.cookie.min.js"></script>

<link href="./css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="./css/rdoc.css" rel="stylesheet">
<link href="./css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
  var index_rel_prefix = "./";
  var darkModeCsseHref = "./css/rdoc-dm.css"
  var defaultModeCssHref = "./css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="class">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0.preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="./index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="./table_of_contents.html#pages">Pages</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    

    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
<div id="parent-class-section" class="nav-section">
  <h3>Parent</h3>

  <p class="link"><a href="BasicSocket.html">BasicSocket</a>
</div>

        
        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-c-getaddress">::getaddress</a>
    <li ><a href="#method-i-addr">#addr</a>
    <li ><a href="#method-i-inspect">#inspect</a>
    <li ><a href="#method-i-peeraddr">#peeraddr</a>
    <li ><a href="#method-i-recvfrom">#recvfrom</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="class-IPSocket">
    <h1 id="class-IPSocket" class="class">
      class IPSocket
    </h1>

    <section class="description">
    
<p><a href="IPSocket.html"><code>IPSocket</code></a> is the super class of <a href="TCPSocket.html"><code>TCPSocket</code></a> and <a href="UDPSocket.html"><code>UDPSocket</code></a>.</p>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">





                <section id="public-class-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Class Methods</h3>
                </header>

                  <div id="method-c-getaddress" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          getaddress(host)        &rarr; ipaddress
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Lookups the IP address of <em>host</em>.</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>

<span class="ruby-constant">IPSocket</span>.<span class="ruby-identifier">getaddress</span>(<span class="ruby-string">&quot;localhost&quot;</span>)     <span class="ruby-comment">#=&gt; &quot;127.0.0.1&quot;</span>
<span class="ruby-constant">IPSocket</span>.<span class="ruby-identifier">getaddress</span>(<span class="ruby-string">&quot;ip6-localhost&quot;</span>) <span class="ruby-comment">#=&gt; &quot;::1&quot;</span>
</pre>

                              <div class="method-source-code" id="getaddress-source">
            <pre>static VALUE
ip_s_getaddress(VALUE obj, VALUE host)
{
    union_sockaddr addr;
    struct rb_addrinfo *res = rsock_addrinfo(host, Qnil, AF_UNSPEC, SOCK_STREAM, 0);
    socklen_t len = res-&gt;ai-&gt;ai_addrlen;

    /* just take the first one */
    memcpy(&amp;addr, res-&gt;ai-&gt;ai_addr, len);
    rb_freeaddrinfo(res);

    return rsock_make_ipaddr(&amp;addr.addr, len);
}</pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Instance Methods</h3>
                </header>

                  <div id="method-i-addr" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          addr([reverse_lookup]) &rarr; [address_family, port, hostname, numeric_address]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the local address as an array which contains address_family, port, hostname and numeric_address.</p>

<p>If <code>reverse_lookup</code> is <code>true</code> or <code>:hostname</code>, hostname is obtained from numeric_address using reverse lookup. Or if it is <code>false</code>, or <code>:numeric</code>, hostname is the same as numeric_address. Or if it is <code>nil</code> or omitted, obeys to <code>ipsocket.do_not_reverse_lookup</code>. See <code>Socket.getaddrinfo</code> also.</p>

<pre class="ruby"><span class="ruby-constant">TCPSocket</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">&quot;www.ruby-lang.org&quot;</span>, <span class="ruby-value">80</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">sock</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">addr</span> <span class="ruby-comment">#=&gt; [&quot;AF_INET&quot;, 49429, &quot;hal&quot;, &quot;192.168.0.128&quot;]</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">addr</span>(<span class="ruby-keyword">true</span>)  <span class="ruby-comment">#=&gt; [&quot;AF_INET&quot;, 49429, &quot;hal&quot;, &quot;192.168.0.128&quot;]</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">addr</span>(<span class="ruby-keyword">false</span>) <span class="ruby-comment">#=&gt; [&quot;AF_INET&quot;, 49429, &quot;192.168.0.128&quot;, &quot;192.168.0.128&quot;]</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">addr</span>(<span class="ruby-value">:hostname</span>)  <span class="ruby-comment">#=&gt; [&quot;AF_INET&quot;, 49429, &quot;hal&quot;, &quot;192.168.0.128&quot;]</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">addr</span>(<span class="ruby-value">:numeric</span>)   <span class="ruby-comment">#=&gt; [&quot;AF_INET&quot;, 49429, &quot;192.168.0.128&quot;, &quot;192.168.0.128&quot;]</span>
}
</pre>

                              <div class="method-source-code" id="addr-source">
            <pre>static VALUE
ip_addr(int argc, VALUE *argv, VALUE sock)
{
    rb_io_t *fptr;
    union_sockaddr addr;
    socklen_t len = (socklen_t)sizeof addr;
    int norevlookup;

    GetOpenFile(sock, fptr);

    if (argc &lt; 1 || !rsock_revlookup_flag(argv[0], &amp;norevlookup))
        norevlookup = fptr-&gt;mode &amp; FMODE_NOREVLOOKUP;
    if (getsockname(fptr-&gt;fd, &amp;addr.addr, &amp;len) &lt; 0)
        rb_sys_fail(&quot;getsockname(2)&quot;);
    return rsock_ipaddr(&amp;addr.addr, len, norevlookup);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-inspect" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          inspect   &rarr; string
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Return a string describing this <a href="IPSocket.html"><code>IPSocket</code></a> object.</p>

                              <div class="method-source-code" id="inspect-source">
            <pre>static VALUE
ip_inspect(VALUE sock)
{
    VALUE str = rb_call_super(0, 0);
    rb_io_t *fptr = RFILE(sock)-&gt;fptr;
    union_sockaddr addr;
    socklen_t len = (socklen_t)sizeof addr;
    ID id;
    if (fptr &amp;&amp; fptr-&gt;fd &gt;= 0 &amp;&amp;
        getsockname(fptr-&gt;fd, &amp;addr.addr, &amp;len) &gt;= 0 &amp;&amp;
        (id = rsock_intern_family(addr.addr.sa_family)) != 0) {
        VALUE family = rb_id2str(id);
        char hbuf[1024], pbuf[1024];
        long slen = RSTRING_LEN(str);
        const char last = (slen &gt; 1 &amp;&amp; RSTRING_PTR(str)[slen - 1] == &#39;&gt;&#39;) ?
            (--slen, &#39;&gt;&#39;) : 0;
        str = rb_str_subseq(str, 0, slen);
        rb_str_cat_cstr(str, &quot;, &quot;);
        rb_str_append(str, family);
        if (!rb_getnameinfo(&amp;addr.addr, len, hbuf, sizeof(hbuf),
                            pbuf, sizeof(pbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
            rb_str_cat_cstr(str, &quot;, &quot;);
            rb_str_cat_cstr(str, hbuf);
            rb_str_cat_cstr(str, &quot;, &quot;);
            rb_str_cat_cstr(str, pbuf);
        }
        if (last) rb_str_cat(str, &amp;last, 1);
    }
    return str;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-peeraddr" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          peeraddr([reverse_lookup]) &rarr; [address_family, port, hostname, numeric_address]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the remote address as an array which contains address_family, port, hostname and numeric_address. It is defined for connection oriented socket such as <a href="TCPSocket.html"><code>TCPSocket</code></a>.</p>

<p>If <code>reverse_lookup</code> is <code>true</code> or <code>:hostname</code>, hostname is obtained from numeric_address using reverse lookup. Or if it is <code>false</code>, or <code>:numeric</code>, hostname is the same as numeric_address. Or if it is <code>nil</code> or omitted, obeys to <code>ipsocket.do_not_reverse_lookup</code>. See <code>Socket.getaddrinfo</code> also.</p>

<pre class="ruby"><span class="ruby-constant">TCPSocket</span>.<span class="ruby-identifier">open</span>(<span class="ruby-string">&quot;www.ruby-lang.org&quot;</span>, <span class="ruby-value">80</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">sock</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">peeraddr</span> <span class="ruby-comment">#=&gt; [&quot;AF_INET&quot;, 80, &quot;carbon.ruby-lang.org&quot;, &quot;221.186.184.68&quot;]</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">peeraddr</span>(<span class="ruby-keyword">true</span>)  <span class="ruby-comment">#=&gt; [&quot;AF_INET&quot;, 80, &quot;carbon.ruby-lang.org&quot;, &quot;221.186.184.68&quot;]</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">peeraddr</span>(<span class="ruby-keyword">false</span>) <span class="ruby-comment">#=&gt; [&quot;AF_INET&quot;, 80, &quot;221.186.184.68&quot;, &quot;221.186.184.68&quot;]</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">peeraddr</span>(<span class="ruby-value">:hostname</span>) <span class="ruby-comment">#=&gt; [&quot;AF_INET&quot;, 80, &quot;carbon.ruby-lang.org&quot;, &quot;221.186.184.68&quot;]</span>
  <span class="ruby-identifier">p</span> <span class="ruby-identifier">sock</span>.<span class="ruby-identifier">peeraddr</span>(<span class="ruby-value">:numeric</span>)  <span class="ruby-comment">#=&gt; [&quot;AF_INET&quot;, 80, &quot;221.186.184.68&quot;, &quot;221.186.184.68&quot;]</span>
}
</pre>

                              <div class="method-source-code" id="peeraddr-source">
            <pre>static VALUE
ip_peeraddr(int argc, VALUE *argv, VALUE sock)
{
    rb_io_t *fptr;
    union_sockaddr addr;
    socklen_t len = (socklen_t)sizeof addr;
    int norevlookup;

    GetOpenFile(sock, fptr);

    if (argc &lt; 1 || !rsock_revlookup_flag(argv[0], &amp;norevlookup))
        norevlookup = fptr-&gt;mode &amp; FMODE_NOREVLOOKUP;
    if (getpeername(fptr-&gt;fd, &amp;addr.addr, &amp;len) &lt; 0)
        rb_sys_fail(&quot;getpeername(2)&quot;);
    return rsock_ipaddr(&amp;addr.addr, len, norevlookup);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-recvfrom" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          recvfrom(maxlen)        &rarr; [mesg, ipaddr]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          recvfrom(maxlen, flags) &rarr; [mesg, ipaddr]
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Receives a message and return the message as a string and an address which the message come from.</p>

<p><em>maxlen</em> is the maximum number of bytes to receive.</p>

<p><em>flags</em> should be a bitwise OR of Socket::MSG_* constants.</p>

<p>ipaddr is the same as IPSocket#{peeraddr,addr}.</p>

<pre class="ruby"><span class="ruby-identifier">u1</span> = <span class="ruby-constant">UDPSocket</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">u1</span>.<span class="ruby-identifier">bind</span>(<span class="ruby-string">&quot;127.0.0.1&quot;</span>, <span class="ruby-value">4913</span>)
<span class="ruby-identifier">u2</span> = <span class="ruby-constant">UDPSocket</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">u2</span>.<span class="ruby-identifier">send</span> <span class="ruby-string">&quot;uuuu&quot;</span>, <span class="ruby-value">0</span>, <span class="ruby-string">&quot;127.0.0.1&quot;</span>, <span class="ruby-value">4913</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">u1</span>.<span class="ruby-identifier">recvfrom</span>(<span class="ruby-value">10</span>) <span class="ruby-comment">#=&gt; [&quot;uuuu&quot;, [&quot;AF_INET&quot;, 33230, &quot;localhost&quot;, &quot;127.0.0.1&quot;]]</span>
</pre>

                              <div class="method-source-code" id="recvfrom-source">
            <pre>static VALUE
ip_recvfrom(int argc, VALUE *argv, VALUE sock)
{
    return rsock_s_recvfrom(sock, argc, argv, RECV_IP);
}</pre>
                              </div>
                            </div>


                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is a service of <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>, purveyors of fine <a href='https://jamesbritt.bandcamp.com/'>dance noise</a></p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

